package com.songlanyun.modules.marketing.groupbuy.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.modules.marketing.groupbuy.entity.GroupBuyActivityGoods;
import com.songlanyun.modules.marketing.groupbuy.entity.GroupBuyGoodsSku;
import com.songlanyun.modules.marketing.groupbuy.model.dto.GroupBuyActivityGoodsDTO;
import com.songlanyun.modules.marketing.groupbuy.model.vo.GroupBuyActivityGoodsVO;
import com.songlanyun.modules.marketing.groupbuy.model.vo.GroupBuyActivityVO;
import com.songlanyun.modules.marketing.seckill.entity.SeckillActivityGoods;
import org.redisson.api.RSemaphore;

import java.util.List;
import java.util.Map;

/**
 * 拼团活动商品表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-27 14:03:38
 */
public interface GroupBuyActivityGoodsService extends IService<GroupBuyActivityGoods> {

    PageUtils goodsPage(Map<String, Object> params);

    void saveGoods(GroupBuyActivityGoodsDTO dto);

    GroupBuyActivityGoods getByGoodsIdAndGroupBuyId(Long goodsId, Long groupBuyId);

    void deleteById(Long id);

    GroupBuyActivityGoods getGoodsAndSkuById(Long id);

    GroupBuyActivityGoods loadActivityGoodsById(Long id,Boolean tw);

    GroupBuyActivityVO groupBuying(Long shopId);

    GroupBuyActivityGoodsVO getGoodsInfo(Long id,Boolean isThrow);

    Integer getStock(Long activityGoodsId);

    /**
     * 判断当前时间段有没有指定拼团活动的商品存在
     *
     * @param groupBuyId 拼团活动id
     * @param goodsId   商品id
     * @return
     */
    Boolean verifyByGroupBuyAndGoods(Long groupBuyId, Long goodsId);

    /**
     * 判断当前时间段有没有指定拼团活动商品存在
     *
     * @param groupBuyGoodsId 拼团活动商品id
     * @return
     */
    Boolean verifyByGroupBuyGoods(Long groupBuyGoodsId);


    List<GroupBuyActivityGoods> getGoodsListBuyGroupBuyId(Long groupBuyId);

    /**
     * 创建并设置信号量
     * @param goods
     */
    void createAndSetSemaphore(GroupBuyActivityGoods goods);

    /**
     * 根据活动商品获取当前的信号
     * @param goods
     * @return
     */
    RSemaphore getSemaphore(GroupBuyActivityGoods goods);

    /**
     * 获取信号量，如果获取成功信号号会减少
     * @param activityId
     * @param activityGoodsId
     * @param goodsId
     * @param size
     * @return
     */
    Boolean tryAcquire(Long activityId,Long activityGoodsId, Long goodsId,int size);

    /**
     * 释放（回退）信号量,
     * @param goods
     * @param size
     */
    void release(GroupBuyActivityGoods goods, int size);

    void release(Long activityId, Long activityGoodsId, Long goodsId, int size);

    /**
     * 获取拼团商品信号量的key
     * @param activityId
     * @param activityGoodsId
     * @param goodsId
     * @return
     */
    static  String getSemaphoreKey(Long activityId,Long activityGoodsId, Long goodsId)
    {
        return "groupbuy:activityId:" + activityId + "activityGoodsId:" + activityGoodsId + "_goodsId:" + goodsId;
    }

    List<GroupBuyGoodsSku> getSkusById(Long groupBuyGoodsId);

    List<GroupBuyActivityGoodsVO> willYouSpell(Map<String, Object> params);

    PageUtils willYouSpellPage(Map<String, Object> params);

    void verifyActiveGoods(Long groupBuyId);

    List<GroupBuyActivityGoodsVO> groupBuyingGoodsList(Long shopId);
}

